package org.example.common;

import lombok.Data;

/**
 * 统一响应结果类
 * @param <T> 数据类型
 */
@Data
public class Result<T> {
    
    private boolean success;
    private String message;
    private T data;
    private String code;
    
    public Result() {}
    
    public Result(boolean success, String message, T data) {
        this.success = success;
        this.message = message;
        this.data = data;
    }
    
    public Result(boolean success, String message, T data, String code) {
        this.success = success;
        this.message = message;
        this.data = data;
        this.code = code;
    }
    
    /**
     * 成功响应
     */
    public static <T> Result<T> success(T data) {
        return new Result<>(true, "操作成功", data, "200");
    }
    
    public static <T> Result<T> success(String message, T data) {
        return new Result<>(true, message, data, "200");
    }
    
    /**
     * 失败响应
     */
    public static <T> Result<T> error(String message) {
        return new Result<>(false, message, null, "500");
    }
    
    public static <T> Result<T> error(String message, String code) {
        return new Result<>(false, message, null, code);
    }
    
    /**
     * 参数错误
     */
    public static <T> Result<T> badRequest(String message) {
        return new Result<>(false, message, null, "400");
    }
    
    /**
     * 未找到
     */
    public static <T> Result<T> notFound(String message) {
        return new Result<>(false, message, null, "404");
    }
}